#include <stdio.h>
#include <string>
#include <regex>
#include <iostream>

// std::regex regIPv4("^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$");

// std::regex regIPv4("^([0-9]{1,3})(.[0-9]{1,3}){3}$");OK
std::regex regIPv4Value("^((2(5[0-5]|[0-4][0-9])))|([0-1]?[0-9]{1,2})$");
// std::regex regIPv4("^((2(5[0-5]|[0-4][0-9])))|([0-1]?[0-9]{1,2})(.[0-9]{1,3}){3}$");

std::regex regIPv4("^((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}$");

bool isIpv4Value(std::string value)
{
    return std::regex_match(value, regIPv4Value);
}

bool isIPv4(std::string ip)
{
    return std::regex_match(ip, regIPv4);
};

int main1(int argn, char **argv)
{
    std::string value = "";
    printf("%s =>%s\n", value.c_str(), isIpv4Value(value) ? "true" : "false");
    value = "127";
    printf("%s =>%s\n", value.c_str(), isIpv4Value(value) ? "true" : "false");
    value = "1";
    printf("%s =>%s\n", value.c_str(), isIpv4Value(value) ? "true" : "false");
    value = "255";
    printf("%s =>%s\n", value.c_str(), isIpv4Value(value) ? "true" : "false");
    value = "256";
    printf("%s =>%s\n", value.c_str(), isIpv4Value(value) ? "true" : "false");

    std::string ip = "";
    ip = "192.168.1.127";
    printf("%s =>%s\n", ip.c_str(), isIPv4(ip) ? "true" : "false");
    ip = "255.255.255.255";
    printf("%s =>%s\n", ip.c_str(), isIPv4(ip) ? "true" : "false");
    ip = "256.255.255.255";
    printf("%s =>%s\n", ip.c_str(), isIPv4(ip) ? "true" : "false");
    ip = "255.255.255.256";
    printf("%s =>%s\n", ip.c_str(), isIPv4(ip) ? "true" : "false");

    printf("Hello World!\n");
    return 0;
}

main(int argn, char **argv)
{
    std::string data = "sdfsadf adfasdfas dfg assdf sadf asdf asdf sad fasd f asdf \
GET /sadfasdfefsd/sd/f/sdf//sdf//sd/f/sd/f/sdsdf HTTP/1.1\r\n\
Host: dev.liuyunuo.cn:12345\r\n\
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36\r\n\
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\n\
Accept-Encoding: gzip, deflate\r\n\
Accept-Language: zh-CN,zh;q=0.9\r\n\
Cookie: pmaCookieVer=5; pma_lang=zh_CN; pma_collation_connection=utf8_general_ci\r\n\
Upgrade-Insecure-Requests: 1\r\n\
";
    std::smatch result;
    std::regex regHttpRequsetLine("(OPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT) ([^ \\s]+) (HTTP/1.[0|1]{1})\r\n");

    while (1)
    {
        if (std::regex_search(data, result, regHttpRequsetLine))
        {
            printf("-------------------------------------\n");
            for (int i = 0; i < result.size(); ++i)
                std::cout << "[" << i << ":" << result.position(i) << ":" << result.length(i) << "]>>>" << result[i] << "<<<" << std::endl;
            data = result.suffix();
            printf("-------------------------------------\n");
        }
        else
        {
            printf("-------------------------------------\n");
            printf("can't find\n");
            printf("-------------------------------------\n");
            break;
        }
    }

    printf("Hello World!\n");
}
